#include "Cats/CatStates/CatState.h"
#include "Cats/CatStates/CatState_Base_Dying.h"
#include "Cats/BaseCat.h"
#include "Cats/CatSpriteLoader.h"

#include "Supporting/SoundLoader.h"
#include "Supporting/Enums/EnumSoundType.h"

CatState_Base_Dying::CatState_Base_Dying(BaseCat* pCatRef):CatState(pCatRef)
{

}

CatState_Base_Dying::~CatState_Base_Dying()
{

}

void CatState_Base_Dying::runOnEnter()
{
	//choose a death animation and calculate its duration
	EnumCatAnimation catAnimationEnum;
	if (_pCatRef->getInjuryType() == INJURY_ZAPPED || _pCatRef->getInjuryType() == INJURY_BURNT)
		catAnimationEnum = CAT_ANIM_DEATH_BLACK;
	else
		catAnimationEnum = CAT_ANIM_DEATH_WHITE;

	_deathAnimationDuration = CatSpriteLoader::getAnimationDuration(_pCatRef->getCatType(), catAnimationEnum);

	// Death Sound
	SoundLoader::playSound(CAT_DEATH_SOUND);

	//play death animation
	_pCatRef->stopAllActions();
	_pCatRef->runAction(CatSpriteLoader::getCatStatusAnimation(catAnimationEnum));
}

void CatState_Base_Dying::update(float timeDiff)
{
	_deathAnimationDuration -= timeDiff;

	//check for end of animation
	if (_deathAnimationDuration <= 0)
	{
		_pCatRef->stopAllActions();
		_pCatRef->setNextCatState(CAT_DEAD);
	}
}

void CatState_Base_Dying::onExit()
{
	SoundLoader::endSound(CAT_DEATH_SOUND);
	CatState::onExit();
}

void CatState_Base_Dying::onPause()
{
	SoundLoader::endSound(CAT_DEATH_SOUND);
}

